﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.AppEngine.IService;
using Empires.RAL.ORM;
using System.Web.Security;

namespace Empires.BLL.AppEngine.Implement
{
    public class UserService : IUserService
    {
        public bool UserLogin(string username, string password, bool isremember)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return true;
            }
            Empires.RAL.Access.IAppUserAccess access = new Empires.RAL.Pump.AppUserDataPump();
            var user = access.GetUserEntity<tb_user>(username);
            if (user != null && !user.c_b_isLock)
            {
                password = Empires.CRL.Tools.HashAlgorithm.Encrypt(password, CRL.Tools.EncryptMode.sha1);
                if (user.c_vc_password == password)
                {
                    FormsAuthentication.SetAuthCookie(username, isremember);
                    return true;
                }
            }
            return false;

        }
        /// <summary>
        /// 存在返回1，否则返回0
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public int ExistUserName(string username)
        {
            Empires.RAL.Access.IAppUserAccess access = new Empires.RAL.Pump.AppUserDataPump();
            return access.UserIsExists(username);
        }
        public int NewUserRegister(string username, string password, string nickname, bool sex, string birthday, string contact, string appellaction, string address)
        {
            DateTime dt = new DateTime();
            try
            {
                dt = DateTime.Parse(birthday);
            }
            catch (FormatException)
            {
                string[] piece = birthday.Split('.');
                if (piece[1] == "2" && Int32.Parse(piece[2]) > 28)
                {
                    dt = DateTime.Parse(piece[0].ToString() + ".2.28");
                }
            }
            Empires.RAL.Access.IAppUserAccess access =
                new Empires.RAL.Pump.AppUserDataPump();
            return access.NewUserRegister(username, Empires.CRL.Tools.HashAlgorithm.Encrypt(password, CRL.Tools.EncryptMode.sha256), nickname, sex, dt, contact, appellaction, address);
        }
        public int GetUserStatus(string username)
        {
            if (String.IsNullOrEmpty(username))
            {
                return -1;
            }
            Empires.RAL.Access.IAppUserAccess access = new Empires.RAL.Pump.AppUserDataPump();
            return access.GetUserStatus(username);
        }
        public string GetUserWebSiteName(string username)
        {
            Empires.RAL.Access.ICompanyBaseAccess access = new Empires.RAL.Pump.CompanyBaseDataPump();
            return access.GetCompanyNameByMasterName(username);
        }
        /// <summary>
        /// 获取昵称
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public string GetUserNickname(string username)
        {
            Empires.RAL.Access.IAppUserAccess access = new Empires.RAL.Pump.AppUserDataPump();
            return access.GetUserNickname(username);
        }
    }
}
